package cn.com.okgotest.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import cn.com.okgo.utils.ThreadUtils;
import cn.com.okgotest.R;
import cn.com.okgotest.fragment.base.BaseFragment;
import okhttp3.Call;


public class OkHttp2Fragment extends BaseFragment {


    private String url = "https://images1.bolanjr.com/bolanadmin/admin/uploadfile/wxtwo/tu/244.jpg?kh=zxjt";

    private TextView requestTv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ok_http2, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        Button okhttp3Btn = (Button) view.findViewById(R.id.okhttp3Btn);
        Button okhttp2Btn = (Button) view.findViewById(R.id.okhttp2Btn);
        requestTv = (TextView) view.findViewById(R.id.requestTv);

        okhttp3Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ThreadUtils.dispatchToSubThread(new Runnable() {
                    @Override
                    public void run() {
                        okhttp3.OkHttpClient client = new okhttp3.OkHttpClient();
                        okhttp3.Request request = new okhttp3.Request.Builder()
                                .url(url)
                                .build();
                        okhttp3.Call call = client.newCall(request);
                        call.enqueue(new okhttp3.Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                setText("error: \n" + e.toString());
                            }

                            @Override
                            public void onResponse(Call call, okhttp3.Response response) throws IOException {
                                setText(response.toString());
                            }
                        });
                    }
                });

            }
        });

        okhttp2Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ThreadUtils.dispatchToSubThread(new Runnable() {
                    @Override
                    public void run() {
                        com.squareup.okhttp.OkHttpClient client = new OkHttpClient();
                        com.squareup.okhttp.Request request = new com.squareup.okhttp.Request.Builder()
                                .url(url)
                                .build();
                        com.squareup.okhttp.Call call = client.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(Request request, IOException e) {
                                setText("error: \n" + request.toString());
                            }

                            @Override
                            public void onResponse(Response response) throws IOException {
                                setText(response.toString());
                            }
                        });
                    }
                });
            }
        });

    }

    private void setText(final String msg){
        ThreadUtils.dispatchToMainThread(new Runnable() {
            @Override
            public void run() {
                requestTv.setText(msg);
            }
        });
    }

}
